package com.colin.datamove.service.impl;

import com.colin.datamove.entity.OpenAccount;
import com.colin.datamove.mapper.db1.OpenAccountDao;
import com.colin.datamove.service.MoveService;
import com.colin.datamove.util.LogUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("openAccountService")
public class OpenAccountServiceImpl implements MoveService {

    @Resource
    private OpenAccountDao db1OpenAccountDao;
    @Resource
    private com.colin.datamove.mapper.db2.OpenAccountDao db2OpenAccountDao;

    @Override
    public void moveJob(int pmc) {
        // 查询老数据库需要迁移数据
        List<OpenAccount> unMoveData = db2OpenAccountDao.getUnMoveData(pmc);
        LogUtil.info("openAccountService 此次待迁移数据:{}条", unMoveData.size());
        if (CollectionUtils.isNotEmpty(unMoveData)) {
            // 更新数据为已迁移
            int i = db2OpenAccountDao.updateMoved(unMoveData);
            LogUtil.info("openAccountService 更新待迁移数据为已迁移:{}条", i);

            // 插入新数据库
            int i1 = db1OpenAccountDao.insertData(unMoveData);
            LogUtil.info("openAccountService 新库中插入数据:{}条", i1);
        }

        int i2 = db1OpenAccountDao.updateRootCompanyId();
        LogUtil.info("openAccountService 新库中更新rootCompanyId数据:{}条", i2);
    }


}
